package com.xjh.basestudy.leetcode.question;

import com.xjh.basestudy.leetcode.tool.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author:XuJianHua
 * @Date:2023/1/5
 * @Description:
 * 给定一个二叉树的根节点 root ，返回 它的 中序 遍历 。
 *
 * 输入：root = [1,null,2,3]
 * 输出：[1,3,2]
 */
public class Q94_inorderTraversal {


    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        TreeNode left = new TreeNode(2);
        TreeNode right = new TreeNode(3);
        root.setLeft(left);
        root.setRight(right);
        List<Integer> integers = inorderTraversal(root);
        System.out.println(integers);

    }
    public static List<Integer>  inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<Integer>();
        dfs(res,root);
        return res;
    }

    static  void  dfs(List<Integer> res, TreeNode root) {
        if(root==null) {
            return;
        }
        //按照 左-打印-右的方式遍历
        dfs(res,root.getLeft());
        res.add(root.getVal());
        dfs(res,root.getRight());
    }

}
